<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增商品型号' : '编辑商品型号'"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" label-width="80px" class="form" :model="temp" :rules="rules">
      <el-form-item label="编码:" prop="specCode">
        <el-input v-model="temp.specCode" :maxlength="20" placeholder="请输入">
          <el-button v-if="type==='add'" slot="append" class="append-search" @click.prevent="getCode">获取</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="排序:" prop="showIndex">
        <el-input-number v-model="temp.showIndex" style="width: 100%;" :min="1" :max="99999999" :precision="0" />
      </el-form-item>
      <el-form-item label="型号:" prop="specName">
        <el-input v-model="temp.specName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="规格:" prop="specAliasCode">
        <el-select v-model="temp.specAliasCode" :loading="$loadingPlugin.selectSpecAliasList" placeholder="请选择">
          <el-option v-for="item in specAliasList" :key="item.specAliasCode" :value="item.specAliasCode" :label="item.specAlias" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注:" prop="remark" class="block">
        <el-input v-model="temp.remark" type="textarea" :rows="5" :maxlength="1000" show-word-limit />
      </el-form-item>
    </el-form>
    <div class="char" style="margin: 10px 0;">
      <p style="color: red;">特别说明</p>
      <p>型号与规格的区别在于型号是在规格基础上再次细化分类</p>
      <p>比如目前有15KG 规格的 智能瓶,普通瓶 两种,这时我们可以将</p>
      <p>15Kg智能瓶 定义为型号 ,15Kg定义为规格</p>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>

  </el-dialog>
</template>

<script>
  import { queryOrgCode, querySpecAliasList, goodsSpecAdd } from '@/api'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        type: 'add',
        specAliasList: [],
        temp: {
          specId: 0,
          specCode: '',
          showIndex: 999,
          specName: '',
          specAliasCode: '',
          remark: ''
        },
        rules: {
          specName: [{ required: true, message: '请输入型号', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.selectSpecAliasList()
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.type = 'edit'
            if (temp.showIndex === 0) this.temp.showIndex = 999
          }
        })
      },
      // 初始化规格列表
      async selectSpecAliasList() {
        const res = await querySpecAliasList()
        this.specAliasList = res.data
      },
      // 获取商品型号编码
      async getCode() {
        const res = await queryOrgCode({
          codeType: 6
        })
        this.temp.specCode = res.data
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const spec = this.specAliasList.find(item => item.specAliasCode === this.temp.specAliasCode)
          const params = {
            specAliasCode: this.temp.specAliasCode,
            specCode: this.temp.specCode,
            specName: this.temp.specName,
            specAlias: spec.specAlias,
            remark: this.temp.remark,
            specId: this.temp.specId,
            showIndex: this.temp.showIndex
          }
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await goodsSpecAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await goodsSpecAdd(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          specId: 0,
          specCode: '',
          showIndex: 999,
          specName: '',
          specAliasCode: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
.char {
  p {
    margin: 5px 0;
  }
}
</style>
